package com.sense.kukoo.common.wiget.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.sense.kukoo.common.R
import com.sense.kukoo.common.utils.KeyBoardUtils

class AddCommentDialog: Dialog {

    private val TAG: String = javaClass.simpleName
    constructor(context: Context) : super(context)
    constructor(context: Context, themeResId: Int) : super(context, themeResId)
    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener?) : super(context, cancelable, cancelListener)

    private lateinit var tv_btn_send: View
    private lateinit var tv_at_channel_name: TextView
    private lateinit var et_input_text: EditText

    private var mListener: (String)->Unit = {}
    private var atName: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        context.setTheme(R.style.bottom_dialog)
        setContentView(R.layout.common_dialog_add_comment)
        //
        findViewById<View>(R.id.fl_add_comment_root).setOnClickListener {
            dismiss()
        }
        //
        tv_btn_send = findViewById<View>(R.id.tv_btn_send)
        tv_at_channel_name = findViewById<TextView>(R.id.tv_at_channel_name)
        et_input_text = findViewById<EditText>(R.id.et_input_text)
        //
        tv_btn_send.setOnClickListener {
            val text = et_input_text.text
            if(text.length > 0){
                mListener(text.toString())
            }
            dismiss()
        }
        //
        if(TextUtils.isEmpty(atName)){
            tv_at_channel_name.visibility = View.GONE
        } else {
            tv_at_channel_name.visibility = View.VISIBLE
            tv_at_channel_name.text = "@${atName}"
        }
    }

    fun setListener(l: (String)->Unit){
        mListener = l
    }

    override fun show() {
        super.show()
        //
        et_input_text.requestFocus()
        KeyBoardUtils.openKeybord(et_input_text, context)
    }

    override fun dismiss() {
        KeyBoardUtils.closeKeybord(et_input_text, context)
        et_input_text.clearFocus()
        //
        super.dismiss()
    }

    fun autoShow(at_name: String?, l: (String)->Unit){
        atName = at_name
        setListener(l)
        show()
    }
}